﻿using System;
using System.IO;

namespace HelloWorldBll
{
    public class Log
    {
        public static void log(string msg)
        {
            var logfile = GetAddr() + "logs.log";
            var sw = new StreamWriter(logfile, true);
            sw.WriteLine(DateTime.Now.ToShortTimeString() + "\t" + msg);
            sw.Close();
        }
        public static void GogByDay(string msg)
        {
            var logfile = GetAddr() + string.Format("{0}.log", DateTime.Today.ToString("yyyyMMdd"));
            var sw = new StreamWriter(logfile, true);
            sw.WriteLine(DateTime.Now.ToShortTimeString() + "\t" + msg);
            sw.Close();
        }
        private static string GetAddr()
        {
            //默认路径
            var result = "D:\\Log\\";
            //从配置文件获取日志路径
            if (System.Configuration.ConfigurationManager.AppSettings["logaddr"] != null)
            {
                result = System.Configuration.ConfigurationManager.AppSettings["logaddr"];
            }
            //若文件夹不存在就创建
            if (!Directory.Exists(result))
            {
                Directory.CreateDirectory(result);
            }
            return result;
        }
    }
}
